<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Model_user extends CI_Model {

	public function __construct(){
		parent::__construct();
	}

	public function add_student() {
		$data = array(
			'email_dn' => $this->input->post('email'),
			'mat_khau' => $this->input->post('password'),
			'level' =>  2,
			'active' => 0 
			);
		$query = $this->db->insert('dang_nhap', $data);
		if($query) {
			return true;
		}else {
			return false;
		}
	}

	public function add_employer() {
		$data = array(
			'email_dn' => $this->input->post('email'),
			'mat_khau' => $this->input->post('password'),
			'level' =>  1,
			'active' => 0 
			);
		$query = $this->db->insert('dang_nhap', $data);
		if($query) {
			return true;
		}else {
			return false;
		}
	}

	public function get_id($email) {
		$this->db->where('email_dn', $email);
		$query = $this->db->get('dang_nhap');
		return $query->row_array();
	}

	public function get_student_name($id) {
		$this->db->where('id_dn', $id);
		$query = $this->db->get('thongtin_sv');
		return $query->row_array();
	}

	public function student_info($mssv){
		$this->db->where('mssv', $mssv);
		$query = $this->db->get('thongtin_sv');
		return $query->row_array();
	}

	public function get_employer_name($id) {
		$this->db->where('id_dn', $id);
		$query = $this->db->get('thongtin_dn');
		return $query->row_array();
	}

	public function add_student_info($id) {
		$email = explode("@", $this->input->post('email'));
		$mssv = $email[0];
		$data = array(
			'mssv' => $mssv,
			'id_dn' => $id,
			'ten' => $this->input->post('ten'),
			'ho' => $this->input->post('ho'),
			'ngay_sinh' => $this->input->post('birthday'),
			'gioi_tinh' => $this->input->post('sex'),
			'sdt' => $this->input->post('phone'),
			'dia_chi' => $this->input->post('address'),
			'avatar' => $this->session->flashdata('message')
		);
		$query = $this->db->insert('thongtin_sv', $data);
		if($query) {
			return true;
		}else {
			return false;
		}
	}


	public function add_employer_info($id) {
		$data = array(
			'id_dn' => $id,
			'ten_cty' => $this->input->post('ten'),
			'quy_mo' => $this->input->post('quymo'),
			'so_luoc' => $this->input->post('soluoc'),
			'logo' => $this->session->flashdata('message'),
			'dia_chi' => $this->input->post('address'),
			'website' => $this->input->post('website'),
			'sdt' => $this->input->post('phone'),
			'nguoi_lh' => $this->input->post('lh'),
			'email_lh' => $this->input->post('email_lh')
		);
		$query = $this->db->insert('thongtin_dn', $data);
		if($query) {
			return true;
		}else {
			return false;
		}
	}

	public function active($id) {
		$data = array(
			'active' => 1 );
		$this->db->where('id_dn', $id);
		$query = $this->db->update('dang_nhap', $data); 
		if($query) {
			return true;
		}else {
			return false;
		}
	}

	public function deactive($id){
		$data = array(
			'active' => 0 );
		$this->db->where('id_dn', $id);
		$query = $this->db->update('dang_nhap', $data); 
		if($query) {
			return true;
		}else {
			return false;
		}
	}

	public function check_login() {
		$this->db->where('email_dn', $this->input->post('email'));
		$this->db->where('mat_khau', md5($this->input->post('password')));
		$this->db->where('active', 1);
		$query = $this->db->get('dang_nhap');

		if($query->num_rows() == 1) {
			return true;
		}else {
			return false;
		}
	}

	public function apply($id_cv, $mssv){
		$data = array(
			'id_cv' => $id_cv,
			'mssv' => $mssv,
			'id_hs' =>$this->input->post('hs_ut')
			);
		$query = $this->db->insert('ung_tuyen', $data);
		if($query) {
			return true;
		}else {
			return false;
		}
	}
	public function check_apply($id_cv, $mssv){
		$this->db->where('id_cv', $id_cv);
		$this->db->where('mssv', $mssv);
		$query = $this->db->get('ung_tuyen');
		if ($query->num_rows() == 1) {
			return true;
		}else{
			return false;
		}
	}

	// public function update_date_start($mssv){
	// 	$data = array('batdau_tt' => date("Y-m-d"));
	// 	$this->db->where('mssv', $mssv);
	// 	$query = $this->db->update('thongtin_sv', $data);
	// 	if($query){
	// 		return true;
	// 	}else{
	// 		return false;
	// 	}
	// }

	public function update_employer_info(){
		$data = array(
			'ten_cty' => $this->input->post('ten'),
			'quy_mo' => $this->input->post('quymo'),
			'so_luoc' => $this->input->post('soluoc'),
			'logo' => $this->session->userdata('logo'),
			'dia_chi' => $this->input->post('address'),
			'website' => $this->input->post('website'),
			'sdt' => $this->input->post('phone'),
			'nguoi_lh' => $this->input->post('lh'),
			'email_lh' => $this->input->post('email_lh'),
			);
		$this->db->where('id_dn', $this->session->userdata('id'));
		$query = $this->db->update('thongtin_dn', $data);
		if($query){
			return true;
		}else{
			return false;
		}
	}

	public function update_student_info(){
		$data = array(
			'ten' => $this->input->post('ten'),
			'ho' => $this->input->post('ho'),
			'ngay_sinh' => $this->input->post('birthday'),
			'gioi_tinh' =>$this->input->post('sex'),
			'sdt' => $this->input->post('phone'),
			'dia_chi' => $this->input->post('address'),
			'avatar' => $this->session->userdata('avatar')
			);
		$this->db->where('id_dn', $this->session->userdata('id'));
		$query = $this->db->update('thongtin_sv', $data);
		if($query){
			return true;
		}else{
			return false;
		}
	}

	public function update_user(){
		$data = array(
			'mat_khau' => $this->input->post('password')
			);
		$this->db->where('id_dn', $this->session->userdata('id'));
		$query = $this->db->update('dang_nhap', $data);
		if($query){
			return true;
		}else{
			return false;
		}
	}

	public function show_user(){
		$query = $this->db->get('dang_nhap');
		return $query->result_array();
	}

	public function delete_user($id){
		$this->db->where('id_dn', $id);
		$query = $this->db->delete('dang_nhap');
		if($query){
			return true;
		}else{
			return false;
		}
	}

	public function list_student(){
		$query = $this->db->get('thongtin_sv');
		return $query->result_array();
	}

	public function list_employer(){
		$query = $this->db->get('thongtin_dn');
		return $query->result_array();
	}

	public function test($mssv){
		$this->db->where('mssv', $mssv);
		$this->db->where('active', 1);
		$query = $this->db->get('ho_so');
		return $query->row_array();
	}

	public function job_hint($test){
		$query = $this->db->query("select * from congviec_tt where match(yeucau,chi_tiet) against('".$test."') and active=1 order by id limit 3");
		return $query->result_array();
	}

	public function student_apply(){
		$query = $this->db->query("select sv.id_dn, sv.ho, sv.ten, ut.mssv, ut.id_hs, hs.ten_hs, ut.id_cv, cv.ten_cv, cv.congviec_bd, cv.congviec_kt, dn.ten_cty 
								   	from ung_tuyen ut inner join ho_so hs, congviec_tt cv,thongtin_sv sv, thongtin_dn dn
									where trangthai_dn=1 
									and trangthai_sv=1
									and ut.id_hs=hs.id
									and sv.mssv=ut.mssv
									and cv.id_dn=dn.id_dn
									and ut.id_cv=cv.id");
		return $query->result_array();
	}

	public function student_not_apply(){
		$query = $this->db->query("select distinct mssv, id_dn, ten, ho, sdt
									from thongtin_sv
									where mssv not in (select mssv from ung_tuyen where trangthai_dn=1 and trangthai_sv=1)");
		return $query->result_array();
	}

	public function add_leader(){
		$data = array(
			'email_dn' => $this->input->post('email'),
			'mat_khau' => $this->input->post('password'),
			'level' =>  3,
			'active' => 1 
			);
		$query = $this->db->insert('dang_nhap', $data);
		if($query) {
			return true;
		}else {
			return false;
		}
	}

	public function add_leader_info($id, $id_dn){
		$data = array(
			'id' => $id,
			'id_dn' => $id_dn,
			'ten' =>  $this->input->post('name')
			);
		$query = $this->db->insert('thongtin_nv', $data);
		if($query) {
			return true;
		}else {
			return false;
		}
	}

	public function get_leader_name($id) {
		$this->db->where('id', $id);
		$query = $this->db->get('thongtin_nv');
		return $query->row_array();
	}

	public function list_leader($id_dn){
		$this->db->where('id_dn', $id_dn);
		$query = $this->db->get('thongtin_nv');
		return $query->result_array();
	}

	public function manager_loader($id_dn){
		$query = $this->db->query("SELECT *, (select count(*) from ung_tuyen where ung_tuyen.id_nv=thongtin_nv.id) as count from thongtin_nv where id_dn=".$id_dn);
		return $query->result_array();
	}

	public function delete_leader($id){
		$this->db->where('id_dn', $id);
		$query = $this->db->delete('dang_nhap');
		if($query){
			return true;
		}else{
			return false;
		}
	}

	public function student_of_leader($id_nv){
		$query = $this->db->query("select * from thongtin_sv as sv, ung_tuyen as ut, congviec_tt as cv where ut.mssv=sv.mssv and ut.id_cv=cv.id and ut.id_nv=".$id_nv);
		return $query->result_array();
	}

	public function list_student_guide($id_cv, $id_nv){
		$query = $this->db->query("select sv.*, ut.id_hs from ung_tuyen as ut, thongtin_sv as sv where ut.id_cv=".$id_cv." and ut.id_nv=".$id_nv." and sv.mssv=ut.mssv order by sv.mssv");
		return $query->result_array();
	}
}